package edu.dayu.config;

import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Component;

@Component
public class CustomizedJobFactory extends AdaptableJobFactory {
    @Autowired
    private AutowireCapableBeanFactory autowireCapableBeanFactory;

    @Override
    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        Object jobInstance = super.createJobInstance(bundle);
        // 因为 job 类是不会被 spring 管理的，如果要在其类里面使用 bean 对象，这个就是当返回实例后，扫描这个类，然后注入 bean。
        autowireCapableBeanFactory.autowireBean(jobInstance);
        return jobInstance;
    }
}
